<template>
  <a-modal
    title="新增关系管理"
    :width="modalWidth()"
    :visible="visible"
    :centered="true"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    class="update-modal"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="关系名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入关系名称" v-decorator="['chName', {rules: [{required: true, message: '请输入关系名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="英文名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入英文名" v-decorator="['enName', {rules: [{required: true, message: '请输入英文名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="特征"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择特征"
            v-decorator="['feature', {rules: [{ required: true, message: '请选择特征！' }]}]">
            <a-select-option v-for="(item,index) in featureData" :key="index" :value="item.code">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="方向:"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择方向"
            v-decorator="['direct', {rules: [{ required: true, message: '请选择方向！' }]}]">
            <a-select-option v-for="(item,index) in directData" :key="index" :value="item.value">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="概念A"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择概念"
            show-search
            :filter-option="searchOption"
            v-decorator="['schemaFrom', {rules: [{ required: true, message: '请选择概念！' }]}]">
            <a-select-option v-for="(item,index) in typeData" :key="index" :value="item.id">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="概念B"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择概念"
            show-search
            :filter-option="searchOption"
            v-decorator="['schemaTo', {rules: [{ required: true, message: '请选择概念！' }]}]">
            <a-select-option v-for="(item,index) in typeData" :key="index" :value="item.id">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="描述"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea
            :rows="4"
            placeholder="请输入描述"
            v-decorator="['describes']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { relationAdd } from '@/api/modular/main/knowl/relation/relationManage'
import { schemaList } from '@/api/modular/main/knowl/schema/schemaManage'
import { register } from '@/utils/drag_modal'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      },
      featureData: [],
      directData: [
        {
          name: 'A->B',
          value: 1
        },
        {
          name: 'B->A',
          value: 2
        },
        {
          name: 'A<->B',
          value: 3
        }
      ],
      typeData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    modalWidth() {
      return document.body.clientWidth * 0.6
    },
    // 初始化方法
    add(record) {
      this.visible = true
      const featureOption = this.$options
      this.featureData = featureOption.filters['dictData']('relation_feature')
      // const typeOption = this.$options
      // this.typeData = typeOption.filters['dictData']('relation_type')
      this.getSchemaList()
      register(true)
    },
    searchOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    getSchemaList() {
      schemaList({}).then((res) => {
        this.typeData = res.data
      })
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof (values[key]) === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          relationAdd(values).then((res) => {
            if (res.success) {
              this.$message.success('新增成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error('新增失败')// + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      register(false)
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
